package org.zoomdev.zoom.dao.utils;

import org.zoomdev.zoom.common.utils.Classes;
import org.zoomdev.zoom.dao.annotations.Column;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class PageColumnInfoBuilder {


    public static Map<Class, PageColumnInfo[]> map = new ConcurrentHashMap<>();

    public static PageColumnInfo[] get(Class clazz) {
        return map.computeIfAbsent(clazz, (k) -> build(k));
    }

    public static PageColumnInfo[] build(Class<?> clazz) {
        List<Field> fields = Classes.getFields(clazz);
        List<PageColumnInfo> list = new ArrayList<>();
        list.add(PageColumnInfo.count);
        for (Field field : fields) {
            if (field.isAnnotationPresent(Column.class)) {
                Column column = field.getAnnotation(Column.class);
                PageColumnInfo inf = new PageColumnInfo();
                inf.setField(field);
                inf.setSelectRaw(column.value());
                list.add(inf);
            }
        }
        return list.toArray(new PageColumnInfo[list.size()]);
    }

}
